package com.oa.ylms.apps.business.file.impl;

import java.util.List;

import com.oa.ylms.apps.business.file.IFileInfoService;
import com.oa.ylms.apps.business.file.IFileSubmitInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.dao.file.IFileSubmitInfoDao;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.file.FileInfoValue;
import com.oa.ylms.apps.entity.file.FileSubmitInfoValue;
import com.oa.ylms.apps.entity.organization.SectionInfoValue;

public class FileSubmitInfoServiceImpl implements IFileSubmitInfoService {
	
	private FileSubmitInfoValue fileSubmitInfo;
	private SectionInfoValue sectionInfo;
	private IFileInfoService fileInfoService;
	private IFileSubmitInfoDao submitInfoDao;

	public int addFileSubmitInfo(StaffInfoValue staffInfo, int fileId,
			String uploadName, String abstracts) throws Exception {
		fileSubmitInfo.setAbstracts(abstracts);
		fileSubmitInfo.setUploadName(uploadName);
		FileInfoValue fileInfo = fileInfoService.queryFileById(fileId);
		fileSubmitInfo.setFileInfo(fileInfo);
		fileSubmitInfo.setStaffInfo(staffInfo);
		fileSubmitInfo.setSubmitTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		return submitInfoDao.addFileSubmitInfo(fileSubmitInfo);
	}

	@Override
	public boolean deleteFileSubmitInfo(String ids)
			throws Exception {
		String[] idsStr = ids.split(",");
		for(String id : idsStr){
			fileSubmitInfo=queryFileSubmitInfoById(Integer.parseInt(id));
			if(fileSubmitInfo != null){
				submitInfoDao.deleteFileSubmitInfo(fileSubmitInfo);
			}else{
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean modifyFileSubmitInfo(int fileSubmitInfoId,
			String uploadName, String abstracts) throws Exception {
		fileSubmitInfo = queryFileSubmitInfoById(fileSubmitInfoId);
		fileSubmitInfo.setUploadName(uploadName);
		fileSubmitInfo.setAbstracts(abstracts);
		return submitInfoDao.editFileSubmitInfo(fileSubmitInfo);
	}

	@Override
	public FileSubmitInfoValue queryFileSubmitInfoById(int fileSubmitInfoId)
			throws Exception {
		fileSubmitInfo.setId(fileSubmitInfoId);
		return submitInfoDao.queryFileSubmitInfoById(fileSubmitInfo);
	}

	@Override
	public List<FileSubmitInfoValue> queryFileSubmitInfoWithPage(Integer fileId, StaffInfoValue staffInfo, String submitTime, Integer sectionId ,int rows, int pageNo) throws Exception {
		if(fileId != null)
			fileSubmitInfo.setFileInfo(fileInfoService.queryFileById(fileId));
		if(staffInfo != null)
			fileSubmitInfo.setStaffInfo(staffInfo);
		if(submitTime != null)
			fileSubmitInfo.setSubmitTime(DateUtil.toDateWithoutHours(submitTime));
		if(sectionId != null)
			sectionInfo.setSectionId(sectionId);
		return submitInfoDao.queryFileSubmitInfoWithPage(fileSubmitInfo, sectionInfo, rows, pageNo);
	}
	
	@Override
	public int countTPages(Integer fileId, StaffInfoValue staffInfo, String submitTime, Integer sectionId ,int rows) throws Exception{
		if(fileId != null)
			fileSubmitInfo.setFileInfo(fileInfoService.queryFileById(fileId));
		if(staffInfo != null)
			fileSubmitInfo.setStaffInfo(staffInfo);
		if(submitTime != null)
			fileSubmitInfo.setSubmitTime(DateUtil.toDateWithoutHours(submitTime));
		if(sectionId != null)
			sectionInfo.setSectionId(sectionId);
		return submitInfoDao.countTPages(fileSubmitInfo, sectionInfo, rows);
	}
	
	@Override
	public int getTotalNumber () throws Exception{
		return submitInfoDao.getTotalNumbers();
	}

	public FileSubmitInfoValue getFileSubmitInfo() {
		return fileSubmitInfo;
	}

	public void setFileSubmitInfo(FileSubmitInfoValue fileSubmitInfo) {
		this.fileSubmitInfo = fileSubmitInfo;
	}

	public IFileInfoService getFileInfoService() {
		return fileInfoService;
	}

	public void setFileInfoService(IFileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}

	public IFileSubmitInfoDao getSubmitInfoDao() {
		return submitInfoDao;
	}

	public void setSubmitInfoDao(IFileSubmitInfoDao submitInfoDao) {
		this.submitInfoDao = submitInfoDao;
	}

	public void setSectionInfo(SectionInfoValue sectionInfo) {
		this.sectionInfo = sectionInfo;
	}

	public SectionInfoValue getSectionInfo() {
		return sectionInfo;
	}

}
